{% extends 'layout/manage.html' %}
{% load static %}
{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/daterangepicker/daterangepicker.css' %}">
{% endblock %}
{% block cotent %}
    <div class="container-fluid">
        <div class="input-group" style="width: 300px;margin-bottom: 20px">
            <span class="input-group-addon">日期范围</span>
            <input type="text" id="rangePicker" class="form-control">
        </div>
        <div class="row">
            <div class="col-xs-8">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <i class="fa fa-search" aria-hidden="true"></i> 筛选
                    </div>
                    <div class="panel-body filter-area">
                        <div id="lineart" style="height: 400px"></div>
                    </div>
                </div>

            </div>
            <div class="col-xs-4">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <i class="fa fa-search" aria-hidden="true"></i> 筛选
                    </div>
                    <div class="panel-body filter-area">
                        <div id="cake" style="height: 400px"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block js %}
    <script src="{% static 'plugin/highcharts/highcharts.js' %}"></script>
    <script src="{% static 'plugin/daterangepicker/moment.min.js' %}"></script>
    <script src="{% static 'plugin/daterangepicker/daterangepicker.js' %}"></script>
    <script>
        var PIE_INFO = "{% url 'pie_info' project_id=request.manage.id %}"
        var LINE_INFO = "{% url 'line_info' project_id=request.manage.id %}"
        $(function () {
            init_cake(moment().format('YYYY-MM-DD'), moment().add(1, 'days').format('YYYY-MM-DD'));
            init_line(moment().format('YYYY-MM-DD'), moment().add(1, 'days').format('YYYY-MM-DD'));
            init_datepicker();
        })

        function init_datepicker() {
            var options = {
                maxDate: moment(),
                alwaysShowCalendars: true,
                showWeekNumbers: true,
                ranges: {
                    '今天': [moment(), moment()],
                    '昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                    '最近7天': [moment().subtract(6, 'days'), moment()],
                    '最近30天': [moment().subtract(29, 'days'), moment()],
                    '本月': [moment().startOf('month'), moment().endOf('month')]
                },
                locale: {
                    format: 'YYYY-MM-DD',
                    separator: ' 至 ',
                    applyLabel: '确定',
                    cancelLabel: '取消',
                    fromLabel: '开始',
                    toLabel: '结束',
                    customRangeLabel: '自定义',
                    weekLabel: 'W',
                    daysOfWeek: ['一', '二', '三', '四', '五', '六', '日'],
                    monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
                    firstDay: 1
                }
            };
            $('#rangePicker').daterangepicker(options, function (start, end, label) {
                // 选择了时间之后，函数自动被触发。
                init_cake(start.format('YYYY-MM-DD'), end.add(1, 'days').format('YYYY-MM-DD'));
                init_line(start.format('YYYY-MM-DD'), end.add(1, 'days').format('YYYY-MM-DD'));
            });
        }

        function init_cake(start, end) {
            var config = {
                chart: {
                    type: 'pie'
                },
                title: {
                    text: null
                },
                credits: {
                    enabled: false // 关闭版权信息
                },
                tooltip: {
                    pointFormat: '{series.name}: <b>{point.y}</b>'
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: false
                        },
                        showInLegend: true
                    }
                },
                series: [{
                    name: 'Brands',
                    colorByPoint: true,
                    data: [{
                        name: 'Internet Explorer',
                        y: 11.84
                    }, {
                        name: 'Firefox',
                        y: 10.85
                    }]
                }]
            }
            $.ajax({
                url: PIE_INFO,
                type: "GET",
                dataType: "JSON",
                data: {"start": start, "end": end},
                success: function (res) {
                    config.series[0].data = res.data;
                    Highcharts.chart('cake', config);
                }
            })
        }

        function init_line(start, end) {
            var config = {
                chart: {
                    type: 'column'
                },
                credits: {
                    enabled: false // 关闭版权信息
                },
                title: {
                    text: null
                },
                xAxis: {
                    categories: []
                },
                yAxis: {
                    min: 0,
                    title: {
                        text: '问题状态统计'
                    },
                    stackLabels: {  // 堆叠数据标签
                        enabled: true,
                        style: {
                            fontWeight: 'bold',
                            color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                        }
                    }
                },
                legend: {
                    align: 'right',
                    x: -30,
                    verticalAlign: 'top',
                    y: 10,
                    floating: true,
                    backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
                    borderColor: '#CCC',
                    borderWidth: 1,
                    shadow: false
                },
                tooltip: {
                    formatter: function () {
                        return '<b>' + this.x + '</b><br/>' +
                            this.series.name + ': ' + this.y + '<br/>' +
                            '总量: ' + this.point.stackTotal;
                    }
                },
                plotOptions: {
                    column: {
                        stacking: 'normal',
                        dataLabels: {
                            enabled: true,
                            color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
                            style: {
                                // 如果不需要数据标签阴影，可以将 textOutline 设置为 'none'
                                textOutline: '1px 1px black'
                            }
                        }
                    }
                },
                series: []
            }
            $.ajax({
                url: LINE_INFO,
                type: "GET",
                dataType: "JSON",
                data: {"start": start, "end": end},
                success: function (res) {
                    config.series = res.series
                    config.xAxis.categories = res.categories
                    var chart = Highcharts.chart('lineart', config);
                }
            })
        }
    </script>
{% endblock %}
